package com.haier.npt.security.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.haier.npt.exception.BizBaseException;
import com.haier.npt.security.cache.SysAppCacheService;
import com.haier.npt.security.dao.SysAppDao;
import com.haier.npt.security.domain.SysApp;
import com.haier.npt.security.event.SysAppChangeEvent;
import com.haier.npt.security.i18n.SecurityExceptionCode;
import com.haier.npt.security.i18n.SecurityMessageCode;
import com.haier.npt.security.service.SysAppService;
import com.haier.npt.security.utils.KeyUtil;
import com.haier.npt.utils.MessageHelper;

@Service
@Transactional(rollbackFor =Exception.class)
public class SysAppServiceImpl implements SysAppService , ApplicationContextAware {
	
	@Autowired
	private SysAppDao sysAppDao ;
	
	private ApplicationContext ctx  ; 
	
	@Autowired
	private SysAppCacheService sysAppCacheService ;
	@Autowired
	private MessageHelper messageHelper;
	
	
	/**
	 * 添加一个app
	 * @param sysApp
	 * @return
	 */
	@Override
	public  SysApp addApp(SysApp sysApp){
		Assert.notNull(sysApp, messageHelper.getMessage(SecurityMessageCode.SYS_APP_NOT_NULL));//"sysApp is not be null"
		
		
		EntityWrapper<SysApp> entityWrapper = new EntityWrapper<>(); 
		entityWrapper.eq("name", sysApp.getName()); 
		
		int count = this.sysAppDao.selectCount(entityWrapper) ; 
		if(count >0 ){
			throw new BizBaseException(SecurityExceptionCode.SYS_APP_EXISTS);
		}
		
		KeyUtil keyUtil = new KeyUtil(); 
		
		//设置编号
		sysApp.setId(IdWorker.getId());
		sysApp.setCreatedate(new Date());
		//appkey 
		sysApp.setApp_key(keyUtil.appKey());
		//appscrect
		sysApp.setApp_secret(keyUtil.appScrect());
		
		sysApp.setCreatedate(new Date());
		
		this.sysAppDao.insert(sysApp);
		
		//发送事件通知
		this.ctx.publishEvent(new SysAppChangeEvent());
		
		return sysApp ;
	} 
	
	
	
	/**
	 * 更新一个app
	 * @param sysApp
	 * @return
	 */
	@Override
	public boolean updateApp(SysApp sysApp){
		Assert.notNull(sysApp, messageHelper.getMessage(SecurityMessageCode.SYS_APP_NOT_NULL));//"sysApp is not be null"
		Assert.notNull(sysApp.getId(), messageHelper.getMessage(SecurityMessageCode.SyS_APPID_NOT_NULL));
		boolean flag = this.sysAppDao.updateById(sysApp) >0 ? true :false;
		//发送事件通知，清空appkey 的缓存
		this.ctx.publishEvent(new SysAppChangeEvent());
		return flag;
	}
	
	/**
	 * 查询所有的sysapp
	 * @return
	 */
	@Override
	public List<SysApp> listApp(){
		/**
		 *  缓存没有查询数据库
		 */
		List<SysApp> apps = sysAppCacheService.listSysAppFromCache() ; 
		if(apps==null){
			apps  =  this.sysAppDao.selectList(null); 
			this.sysAppCacheService.saveSysAppCache(apps);
		}
		return apps ; 
	}
	
	/**
	 * 验证appkey 的有效性
	 * @param appKey
	 * @param secret
	 * @return
	 */
	@Override
	public SysApp valiteAppKey(String appKey ,String secret) {
		//取出所有的app 
		List<SysApp> sysApps = this.listApp() ; 
		for (SysApp sysApp : sysApps) {
			//如果有 ，继续判断该appkey 是否过期
			if(sysApp.getApp_key().equals(appKey) && sysApp.getApp_secret().equals(secret)){
				
				if(sysApp.isExpire()){
					throw new BizBaseException(SecurityExceptionCode.SYS_APP_EXPIRE);
			}
				return sysApp  ; 
			}
		}
		throw new BizBaseException(SecurityExceptionCode.SYS_APP_NOT_EXISTS) ;
	}
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.ctx = applicationContext ; 
	} 

}
